const getAccessToken = require("./getAccessToken.js");
const rp = require("request-promise");


const callCloudFn = async (ctx, fnNanme, params) => {
  const ACCESS_TOKEN = await getAccessToken();
  const url = `https://api.weixin.qq.com/tcb/invokecloudfunction?access_token=${ACCESS_TOKEN}&env=${ctx.state.env}&name=${fnNanme}`;
  const options = {
    method: "POST",
    uri: url,
    body: {
      ...params
    },
    json: true
  };

  return await rp(options)
  .then((res) => {
    return res;
  })
  .catch((err) => {
    
  });

};

module.exports = callCloudFn;